﻿using LD.Host;
using LdCommunication.Core.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.BC
{
    public partial class FrmPointSet : Form
    {
        private int _editIndex = -1;
        public FrmPointSet()
        {
            InitializeComponent();
            this.dgv_io.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            Appbus.PartNo.PartNoChange += PartNo_PartNoChange;
        }

        private void PartNo_PartNoChange(TB_PartNo partNo)
        {
            Init();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            FrmAxisPointEdit pointAdd = new FrmAxisPointEdit();
            pointAdd.ActionPoint += Add;
            pointAdd.ShowDialog();
        }
        private void Add(TB_Point point)
        {
            this.dgv_io.Rows.Add(point.Name, point.ToString());

            Appbus.PartNo.Config.AxisPoint.Add(point);
            Appbus.PartNo.Save();
        }
        private void Edit(TB_Point point)
        {
            if (_editIndex >= 0)
            {
                this.dgv_io["点位名称", _editIndex].Value = point.Name;
                this.dgv_io["轴", _editIndex].Value = point.ToString();

                var model = Appbus.PartNo.Config.AxisPoint.Where(p => p.Name == point.Name).FirstOrDefault();
                if (model != null)
                {
                    model.Sort = point.Sort;
                    model.AxisPointDic = point.AxisPointDic;
                }

                Appbus.PartNo.Save();
            }
        }
        private void btn_search_Click(object sender, EventArgs e)
        {
            var rows = this.dgv_io.SelectedRows;
            if (rows.Count > 0)
            {
                _editIndex = rows[0].Index;

                var model = Appbus.PartNo.Config.AxisPoint.Where(p => p.Name == rows[0].Cells["点位名称"].Value.ToString()).FirstOrDefault();

                FrmAxisPointEdit pointEdit = new FrmAxisPointEdit(model);
                pointEdit.ActionPoint += Edit;
                pointEdit.ShowDialog();
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            var rows = this.dgv_io.SelectedRows;
            if (rows.Count > 0)
            {
                var point = Appbus.PartNo.Config.AxisPoint
                    .Where(p => p.Name == rows[0].Cells["点位名称"].Value.ToString()).FirstOrDefault();
                Appbus.PartNo.Config.AxisPoint.Remove(point);
                this.dgv_io.Rows.Remove(rows[0]);
                Appbus.PartNo.Save();
            }
        }

        private void FrmIoSet_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            this.dgv_io.Rows.Clear();
            if (Appbus.PartNo.Config != null)
            {
                var pointList = Appbus.PartNo.Config.AxisPoint.OrderBy(p => p.Sort).ToList();
                foreach (var item in pointList)
                {
                    int index = this.dgv_io.Rows.Add();

                    this.dgv_io["点位名称", index].Value = item.Name;
                    this.dgv_io["轴", index].Value = item.ToString();
                }
            }
        }
    }
}
